package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsMeter;
import com.ningxia.water.entity.TsWellMeter;
import com.ningxia.water.mapper.TsMeterMapper;
import com.ningxia.water.mapper.TsWellMeterMapper;
import com.ningxia.water.service.ITsMeterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.vo.out.MeterInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsMeterServiceImpl extends ServiceImpl<TsMeterMapper, TsMeter> implements ITsMeterService {

    @Autowired
    private TsMeterMapper meterMapper;

    @Autowired
    private TsWellMeterMapper wellMeterMapper;

    /**
     * 根据机井id获取设备详情
     *
     * @param wellId
     * @return
     */
    @Override
    public List<MeterInfoVo> findByWellId(Integer wellId) {
        return meterMapper.findByWellId(wellId);
    }

    /**
     * 根据设备状态获取设备总数
     *
     * @param status
     * @return
     */
    @Override
    public Integer findByStatus(int status) {
        return meterMapper.findByStatus(status);
    }


    @Override
    public boolean deleteMeter(Integer meterId) {
        int f1 = meterMapper.deleteById(meterId);
        int f2 = wellMeterMapper.deleteByMeterId(meterId);
        if (f1 > 0 && f2 > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertMeterInfo(TsMeter meter, Integer wellId) {
        //添加设备
        int f1 = meterMapper.insert(meter);
        //设备与机井关联
        Integer meterId = meterMapper.selectMeterIdByMeterCode(meter.getMeterCode());
        int f2 = wellMeterMapper.insert(new TsWellMeter(wellId, meterId));
        return f1 > 0 && f2 > 0;
    }
}
